Option Explicit On 

Imports System.IO
Imports System.Data.OleDb
Imports System.Collections.Specialized

Public Class MyDictionary

   Public Records As SortedList
   Public RaiseErrors As Boolean = False

   Public Sub New()
      Initialize()
   End Sub

   Function LoadINI(ByVal FilePath)

      LoadINI = False

      Initialize()

      Dim Entries
      Entries = LoadTextFile(FilePath)

      Dim arrEntries
      arrEntries = Split(Entries, vbNewLine)

      Dim Entry
      For Each Entry In arrEntries
         ' Remove comments and empty lines
         Entry = Trim(Entry)
         If Left(Entry, 1) <> "#" And Entry <> "" Then
            ' Must have a key
            If InStr(Entry, "=") > 1 Then
               Records.Add(Trim(Left(Entry, InStr(Entry, "=") - 1)), Trim(Mid(Entry, InStr(Entry, "=") + 1)))
            End If
         End If
      Next

      LoadINI = True

   End Function

   Function LoadRows(ByVal Conn As OleDbConnection, ByVal SQLString As String) As Boolean
      ' Load a 2xN Recordset as a Collection

      LoadRows = False

      Initialize()

      Dim DA As New OleDbDataAdapter
      DA.SelectCommand = New OleDbCommand(SQLString, Conn)
      Dim RecordDataSet As New System.Data.DataSet
      DA.Fill(RecordDataSet)

      Dim myRow As System.Data.DataRow
      For Each myRow In RecordDataSet.Tables(0).Rows
         If Not Records.ContainsKey(myRow(0).ToString()) Then
            Records.Add(myRow(0).ToString(), myRow(1).ToString())
         End If
      Next

      LoadRows = True

   End Function

   Function LoadColumns(ByVal Conn As OleDbConnection, ByVal SQLString As String) As Boolean
      ' Load a Nx1 Recordset as a Collection with Column

      LoadColumns = False

      Initialize()

      Dim DA As New OleDbDataAdapter
      DA.SelectCommand = New OleDbCommand(SQLString, Conn)
      Dim RecordDataSet As New System.Data.DataSet
      DA.Fill(RecordDataSet)

      Dim myRow As System.Data.DataRow
      Dim myCol As System.Data.DataColumn
      myRow = RecordDataSet.Tables(0).Rows(0)
      For Each myCol In RecordDataSet.Tables(0).Columns
         If Not Records.ContainsKey(myCol.ColumnName) Then
            Records.Add(myCol.ColumnName, myRow(myCol).ToString())
         End If
      Next

      LoadColumns = True

   End Function

   Overrides Function ToString() As String

      Dim key, temp
      For Each key In Records.Keys
         temp = temp & key & "=" & Records(key) & vbNewLine
      Next

      ToString = temp

   End Function

   '''''''''''''''''''''''''''''''''''''
   ' Properties
   '''''''''''''''''''''''''''''''''''''
   Default Public Property Record(ByVal Key As String) As String

      Get
         If Records.ContainsKey(Key) Then
            Return Records(Key)
         ElseIf RaiseErrors Then
            Throw New Exception("Unable to find key '" & Key & "' in MyDictionary.Records()")
         End If
      End Get

      Set(ByVal Value As String)
         If Records.ContainsKey(Key) Then
            Records(Key) = Value
         Else
            Records.Add(Key, Value)
         End If
      End Set

   End Property

   '''''''''''''''''''''''''''''''''''''
   ' Private Methods
   '''''''''''''''''''''''''''''''''''''

   Sub Initialize()
      ' Create the records object
      Records = New SortedList
   End Sub

   Function LoadTextFile(ByVal filename As String)
      ' Load data from filename

      Dim Reader As StreamReader = New StreamReader(filename)
      Return Reader.ReadToEnd()

   End Function

End Class

